// api/videoFusionService.js

import config from '@/config/app.js'

const BASE_URL = config.AI_HTTP_REQUEST_URL;

/**
 * 获取认证token
 */
const getAuthToken = () => {
	return uni.getStorageSync('token') || '';
};

/**
 * 提交视频人脸融合任务
 * @param {Object} data - 提交参数，需符合后端接口要求
 * @returns {Promise<Object>}
 */
export const submitVideoFusion = async (data) => {
	return new Promise((resolve, reject) => {
		uni.showLoading({
			title: '提交中...',
			mask: true
		});
		uni.request({
			url: `${BASE_URL}/videoFusion/submit`,
			method: 'POST',
			data,
			header: {
				'Content-Type': 'application/json',
				'token': getAuthToken()
			},
			success: (res) => {
				uni.hideLoading();
				if (res.statusCode === 200 && res.data.code === 200) {
					resolve({
						success: true,
						data: res.data.data,
						message: res.data.message
					});
				} else {
					uni.showToast({
						title: res.data.message || '提交失败',
						icon: 'error',
						duration: 3000
					});
					resolve({
						success: false,
						error: res.data.message || '提交失败'
					});
				}
			},
			fail: (err) => {
				uni.hideLoading();
				uni.showToast({
					title: err.errMsg || '网络错误',
					icon: 'error',
					duration: 3000
				});
				resolve({
					success: false,
					error: err.errMsg || '网络错误'
				});
			}
		});
	});
};

/**
 * 查询视频人脸融合任务状态
 * @param {Object} params - 查询参数，如 { jobId: 'xxx' }
 * @returns {Promise<Object>}
 */
export const getVideoFusionStatus = async (params) => {
	return new Promise((resolve, reject) => {
		uni.showLoading({
			title: '查询中...',
			mask: true
		});
		uni.request({
			url: `${BASE_URL}/videoFusion/status`,
			method: 'GET',
			data: params,
			header: {
				'Content-Type': 'application/json',
				'token': getAuthToken()
			},
			success: (res) => {
				uni.hideLoading();
				if (res.statusCode === 200 && res.data.code === 200) {
					resolve({
						success: true,
						data: res.data.data,
						message: res.data.message
					});
				} else {
					uni.showToast({
						title: res.data.message || '查询失败',
						icon: 'error',
						duration: 3000
					});
					resolve({
						success: false,
						error: res.data.message || '查询失败'
					});
				}
			},
			fail: (err) => {
				uni.hideLoading();
				uni.showToast({
					title: err.errMsg || '网络错误',
					icon: 'error',
					duration: 3000
				});
				resolve({
					success: false,
					error: err.errMsg || '网络错误'
				});
			}
		});
	});
};